package com.hipmarket.platform.security.storage

import com.hipmarket.platform.security.JwtAuthenticationUser
import com.hipmarket.platform.security.JwtAuthenticationUserWrapper
import com.hipmarket.platform.security.exception.InvalidTokenException

class JwtAuthenticationMemoryStorage: JwtAuthenticationStorage() {

    private val authenticationMap = mutableMapOf<String, JwtAuthenticationUserWrapper>()

    override fun findUserByToken(token: String): JwtAuthenticationUser {
        val wrapper = authenticationMap[token]?: throw InvalidTokenException()
        println("Token: " + token)
        println("user: " + wrapper.authentication)
        return wrapper.authentication
    }

    override fun login(user: JwtAuthenticationUser): String {
        val token = createToken(user)
        authenticationMap.set(token, JwtAuthenticationUserWrapper(user::class.java.toGenericString(), user))
        return token
    }

    override fun logout(token: String) {
        authenticationMap.remove(token)
    }


}